import {Component, Injector, OnInit} from '@angular/core';
import {Product, ProductService} from '../../../service/product/product.service';
import {AnotherProductService} from '../../../service/another-product/another-product.service';

@Component({
  selector: 'app-product2',
  templateUrl: './product2.component.html',
  styleUrls: ['./product2.component.css'],
  /*
    服务提供者，实例化对象是ProductService 但是返回的对象是useClass对应的类
    声明在组件中提供器，和生命在模块的provide一致时，组件的provide被使用
    组件的提供器，供自己和子组件使用
  */
  providers: [{
    provide: ProductService, useClass: AnotherProductService
  }]
})
export class Product2Component implements OnInit {

  product: Product;

  private productService: ProductService;

  /*ProductService 是令牌 找出对应的实例化对象 这里返回的AnotherProductService 而不是AppModule对应的ProductService 对象*/
  constructor(private inject: Injector) {
    this.productService = inject.get(ProductService);
  }

  ngOnInit() {
    this.product = this.productService.getProduct();
  }
}
